import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/show")
public class ShowServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");

        // 将 生成的响应的body 放入到 respBody 中
        StringBuilder respBody = new StringBuilder();
        respBody.append(req.getProtocol()+"<br>");
        respBody.append(req.getMethod()+"<br>");
        respBody.append(req.getRequestURI()+"<br>");
        respBody.append(req.getContextPath()+"<br>");

        respBody.append("<h1>headers:</h1>");
        Enumeration<String> enumeration = req.getHeaderNames();
        while(enumeration.hasMoreElements()){
            String headerName = enumeration.nextElement();
            respBody.append(headerName + ": ");
            respBody.append(req.getHeaders(headerName)+"<br>");
        }

        resp.getWriter().write(respBody.toString());
    }
}
